package org.javason;

import java.io.StringWriter;

import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.WriterAppender;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.javason.jsonrpc.JsonRpcClient;
import org.javason.util.Metrics;
import org.javason.util.MetricsConfiguration;

import junit.framework.TestCase;

public class TestMetricsThreshold extends TestCase {

	public void testInitialMetrics() {
		StringWriter strWriter = new StringWriter();
		Logger.getLogger(Metrics.class).addAppender(new WriterAppender(new SimpleLayout(), strWriter));
		
		(new MetricsConfiguration()).setGlobalMinThreshold(0.1);
		strWriter.getBuffer().delete(0, strWriter.getBuffer().length());
		try {
			ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("com/rhg/service/test_metrics.xml");
			Metrics metrics3 = Metrics.beginCollection();
			Thread.sleep(200);
			metrics3.endAndLogCollection();
			
			System.out.println("APPENDER: " + strWriter.toString());
			assertTrue (strWriter.toString().length() > 0);
			assertEquals(-1, strWriter.toString().indexOf("arguments"));
			
		} catch (Exception exp) {
			exp.printStackTrace();
		}
		
		strWriter.getBuffer().delete(0, strWriter.getBuffer().length());
		try {
			ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("com/rhg/service/test_metrics.xml");
			Metrics metrics3 = Metrics.beginCollection();			
			metrics3.endAndLogCollection();
			
			System.out.println("APPENDER: " + strWriter.toString());
			assertEquals(-1, strWriter.toString().indexOf("arguments"));
			assertTrue (strWriter.toString().length() == 0);
		} catch (Exception exp) {
			exp.printStackTrace();
		}
		
	}
}
